<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Models\User;
use App\Models\Status;

use Auth;
use Mail;
class UsersController extends Controller
{
    //

     public function __construct()
     {
       # code...
       $this->middleware('auth',[
         'except'=>['show','create','store','index','confirmEmail']
       ]);
       $this->middleware('guest',[
         'only'=>['create']
       ]);
     }


    public function index()
    {

      $users = User::paginate(10);
    return view('users.index', compact('users'));
    }
    public function show(User $user)
    {
      # code...
      $statuses=$user->statuses()
                     ->orderBy('created_at','desc')
                     ->paginate(30);

      return view('users.show', compact('user','statuses'));
    }


    //注册用户view
    public function create ()
    {
      # code...
       return view('users.create');
    }

    //存储注册用户post 请求
    public function store(Request $request)
    {
      # code...
      $this->validate($request ,[
        'name'=>'required |max:50',
        'email'=>'required |email|unique:users|max:255',
        'password'=>'required'
      ]);

     $user = User::create([
       'name'=>$request->name,
       'email'=>$request->email,
       'password'=>bcrypt($request->password),
     ]);
    $this->sendEmailConfirmationTo($user);


     session()->flash('success','验证邮件已经发送到 你注册的邮箱上，请查看');

     return redirect()->route('users.show',[$user]);
    }

    protected function sendEmailConfirmationTo($user)
    {
      $view='emails.confirm';
      $data =compact('user');
      $from ='xiang_chunxiao@126.com';
      $name= 'xcx';
      $to=$user->email;
      $subject ='感谢注册wasan应用 请确认你的邮箱';
      Mail::send($view,$data,function($message) use($from,$name,$to,$subject){
        $message->from($from,$name)->to($to)->subject($subject);
      });
    }

    public function confirmEmail($token)
    {

      $user = User::where('activation_token',$token)->firstOrFail();
      $user->activated=true;
      $user->activation_token=null;
      $user->save();
      Auth::login($user);
      session()->flash('success','恭喜，激活成功');
      return redirect()->route('users.show',[$user]);

    }
    //编辑用户view
    public function edit(User $user)
    {
      # code...
       $this->authorize('update',$user);
      return view('users.edit',compact('user'));
    }


    //更新用户
    public function update(Request $request, User $user)
    {
      # code...

      $this->validate($request, [
           'name' => 'required|max:50',
           'password' => 'nullable|confirmed|min:6'
       ]);
       $this->authorize('update',$user);
       $data = [];
       $data['name'] = $request->name;
       if ($request->password) {
           $data['password'] = bcrypt($request->password);
       }
       $user->update($data);

       session()->flash('success', '个人资料更新成功！');

       return redirect()->route('users.show', $user->id);

    }

    public function destroy(User $user)
    {
       $user->delete();
       session()->flash('success','成功删除用户');
       return back();

    }

    public function followings(User $user)
    {
       $users = $user->followings()->paginate(30);
       $title= "关注的人";
       return view('users.show_follow',compact('users','title'));
    }


    public function followers(User $user)
    {
       $users = $user->followers()->paginate(30);
       $title= "粉丝";
       return view('users.show_follow',compact('users','title'));
    }
}
